package com.huluobo.day11_im.addGroup.view;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.huluobo.day11_im.IMApp;
import com.huluobo.day11_im.R;
import com.huluobo.day11_im.activity.ChatActivity;
import com.huluobo.day11_im.adapter.AddGroupAdapter;
import com.huluobo.day11_im.addGroup.presenter.AddGroupContract;
import com.huluobo.day11_im.addGroup.presenter.AddGroupPresenter;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMConversation;
import com.hyphenate.chat.EMGroupInfo;
import com.hyphenate.chat.EMGroupManager;
import com.hyphenate.chat.EMGroupOptions;

import java.util.ArrayList;
import java.util.List;

public class AddGroupActivity extends AppCompatActivity implements AddGroupContract.View {

    private EditText groupName, groupDesc;
    private AddGroupPresenter presenter;

    private RecyclerView recyclerView;

    private AddGroupAdapter adapter;

    private List<EMGroupInfo> groupInfoList = new ArrayList<>();

    private String groupId;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_group);
        initView();
    }

    private void initView() {
        groupName = findViewById(R.id.addGroup_groupName);
        groupDesc = findViewById(R.id.addGroup_groupDesc);
        presenter = new AddGroupPresenter(this);
        recyclerView = findViewById(R.id.addGroup_recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new AddGroupAdapter(R.layout.item_add_group, groupInfoList);
        recyclerView.setAdapter(adapter);
        adapter.setOnAddClick(new AddGroupAdapter.OnAddClick() {
            @Override
            public void addClick(String groupId) {
                AddGroupActivity.this.groupId = groupId;
                presenter.joinGroup(groupId);
            }
        });
    }

    public void findAllGroups(View view) {
        presenter.findAllGroups(10, null);
    }

    public void createGroup(View view) {
        EMGroupOptions emGroupOptions = new EMGroupOptions();
        emGroupOptions.maxUsers = 100;
        emGroupOptions.style = EMGroupManager.EMGroupStyle.EMGroupStylePublicOpenJoin;
        presenter.createGroup(groupName.getText().toString(), groupDesc.getText().toString(),
                new String[]{EMClient.getInstance().getCurrentUser()}, "这是创建群", emGroupOptions);
    }

    @Override
    public void onCreateGroupSuccess() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(AddGroupActivity.this, "创建群成功", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void onCreateGroupFailed(String error) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(AddGroupActivity.this, "创建群失败:" + error, Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void onFindAllGroupsSuccess(List<EMGroupInfo> groupsList) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                groupInfoList.clear();
                groupInfoList.addAll(groupsList);
                adapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    public void onFindAllGroupsFailed(String error) {
        Toast.makeText(this, "查找群错误:" + error, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onJoinGroupSuccess() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Intent intent = new Intent(AddGroupActivity.this, ChatActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString(IMApp.CHAT_CONVERSATION_ID, groupId);
                bundle.putInt(IMApp.CHAT_TYPE, IMApp.CHAT_TYPE_GROUP);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }

    @Override
    public void onJoinGroupFailed(String error) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (TextUtils.equals(error, "601")) {
                    Intent intent = new Intent(AddGroupActivity.this, ChatActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putString(IMApp.CHAT_CONVERSATION_ID, groupId
                    );
                    bundle.putInt(IMApp.CHAT_TYPE, IMApp.CHAT_TYPE_GROUP);
                    intent.putExtras(bundle);
                    startActivity(intent);
                } else {
                    Toast.makeText(AddGroupActivity.this, "加入群失败:" + error, Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}